package JXY01;

import java.util.Arrays;
import java.util.Scanner;

//猜字母，字母对的有几个，同时位置对的有几个
public class Letter_Guess {
    public static void main(String[] args){
        //输入字符串,填入cs
        Scanner console = new Scanner(System.in);
        String str = console.nextLine();
        char[] cs = new char[str.length()];
        for(int i = 0; i < cs.length; i++){
            cs[i] = str.charAt(i);
        }
        System.out.println("输入数组为"+Arrays.toString(cs));

        char[] cs2 = getChars();
        System.out.println("随机数组为"+Arrays.toString(cs2));
        int[] result = compare(cs,cs2);
        System.out.println("字母正确"+result[0]+"位置正确"+result[1]);

    }
    //随机产生5个按随机顺序排列的字符
    public static char[] getChars(){
        char[] cs = new char[5];
        for (int i = 0; i < cs.length; i++){
            cs[i] =(char)(Math.random() * 26 + 'A');
            //可加入arr[26],值为1表示已有，判断是否重复
        }
        return cs;
    }
    //对比两个字符数组
    public static int[] compare(char[] ch1, char[] ch2){
        //ch1为随机产生的数组，ch2为输入的猜测数组
        int num = 0;        //正确个数
        int count = 0;      //位置正确数

        for (int i = 0; i < ch1.length; i++){
            for (int j = 0; j < ch1.length; j++){
                //j带入ch2[j]进行和ch[i]的比对，第一次判断时同时得出是否位置相同
                if (ch1[i] == ch2[i]) {
                    count++;
                    num++;
                    break;
                }else if (ch1[i] == ch2[j]) {
                    num++;
                }
            }
        }
        int[] result = {num,count};
        return result;
    }
}
